﻿using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Security;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using HandyControl.Controls;
using HandyControl.Data;
using HandyControl.Tools;
using YY.Admin.Views;
using YY.Admin.Core.Models;
using YY.Admin.Services;

namespace YY.Admin.ViewModels.Login
{
    // ViewModels/LoginViewModel.cs
    public class LoginViewModel : BaseViewModel
    {
        private readonly IAuthService _authService;
        private readonly IRegionManager _regionManager;
        private string _username = "admin";
        private string _password = "123456";
        private bool _rememberMe;
        private string _loginMessage = string.Empty;

        public LoginViewModel(IAuthService authService, IRegionManager regionManager)
        {
            _authService = authService;
            _regionManager = regionManager;
            Title = "系统登录";

            LoginCommand = new DelegateCommand(async () => await LoginAsync(), CanLogin)
                .ObservesProperty(() => Username)
                .ObservesProperty(() => Password)
                .ObservesProperty(() => IsLoading);
        }

        public string Username
        {
            get => _username;
            set => SetProperty(ref _username, value);
        }

        public string Password
        {
            get => _password;
            set => SetProperty(ref _password, value);
        }

        public bool RememberMe
        {
            get => _rememberMe;
            set => SetProperty(ref _rememberMe, value);
        }

        public string LoginMessage
        {
            get => _loginMessage;
            set => SetProperty(ref _loginMessage, value);
        }

        public DelegateCommand LoginCommand { get; }

        private bool CanLogin()
        {
            return !string.IsNullOrWhiteSpace(Username) &&
                   !string.IsNullOrWhiteSpace(Password) &&
                   !IsLoading;
        }

        private async Task LoginAsync()
        {
            IsLoading = true;
            LoginMessage = string.Empty;

            try
            {
                var request = new LoginRequest
                {
                    Username = Username,
                    Password = Password,
                    RememberMe = RememberMe
                };

                var response = await _authService.LoginAsync(request);

                if (response.Success)
                {
                    // 登录成功，打开主窗口
                    var container = ContainerLocator.Current;
                    var mainWindow = container.Resolve<MainWindow>();
                    Application.Current.MainWindow?.Hide();
                    Application.Current.MainWindow = mainWindow;
                    //把窗口和RegionManager 绑定一下即可
                    RegionManager.SetRegionManager(mainWindow, _regionManager);
                    mainWindow.Show();
                }
                else
                {
                    LoginMessage = response.Message;
                }
            }
            catch (Exception ex)
            {
                LoginMessage = $"登录出错：{ex.Message}";
            }
            finally
            {
                IsLoading = false;
            }
        }
    }
}